import { postUsersListData, deleteUserById, newUserData, editUserData, postPageListData, deletePageById, newPageData, editPageData } from '@/service/main/system/systetm'
import { defineStore } from 'pinia'
import type { ISystemState } from './type'

const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0,

    pageList: [],
    pageTotalCount: 0,
  }),
  actions: {
    /**
     * 用户
     */
    async postUsersListAction(queryInfo: any) {
      const usersListResult = await postUsersListData(queryInfo)
      const { totalCount, list } = usersListResult.data
      this.usersList = list
      this.usersTotalCount = totalCount
    },
    async deleteUsersByIdAction(id: number) {
      const deleteResult = await deleteUserById(id)
      // 删除成功
      this.postUsersListAction({ offset: 0, size: 10 })
    },
    async newUsersDataAction(userInfo: any) {
      // 1. 创建新用户
      const newResult = await newUserData(userInfo)
      // 刷新列表
      this.postUsersListAction({ offset: 0, size: 10 })
    },
    async editUsersDataAction(id: number, userInfo: any) {
      // 1. 编辑用户
      const editResult = await editUserData(id, userInfo)
      // 2. 刷新列表
      this.postUsersListAction({ offset: 0, size: 10 })
    },

    /** 针对页面进行 增删改查 */
    async postPageListAction(pageName: string, queryInfo: any) {
      const pageListResult = await postPageListData(pageName, queryInfo)
      const { totalCount, list } = pageListResult.data
      this.pageList = list
      this.pageTotalCount = totalCount
    },
    async deletePageByIdAction(pageName: string, id: number) {
      const deleteResult = await deletePageById(pageName, id)
      // 删除成功
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    },
    async newPageDataAction(pageName: string, pageInfo: any) {
      // 1. 创建新用户
      const newResult = await newPageData(pageName, pageInfo)
      // 刷新列表
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    },
    async editPageDataAction(pageName: string, id: number, pageInfo: any) {
      // 1. 编辑用户
      const editResult = await editPageData(pageName, id, pageInfo)
      // 2. 刷新列表
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    }
  }
})

export default useSystemStore